﻿using DeliveryService.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLAllThings
    {
        /// <summary>
        /// 根据学生Id获取其购物车内所有商品
        /// </summary>
        /// <param name="studentId"></param>
        /// <returns></returns>
        public string GetAllThingsFromStudentId(int studentId)
        {
            AllThingsInCar allThings = new AllThingsInCar();
            List<MShoppingCart> shoppingCarts = new List<MShoppingCart>();
            List<MShoppingCartFoods> foodsCarts = new List<MShoppingCartFoods>();

            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                # region 添加商品
                // 通过学生Id获取所有的shoppingCarts
                // 暂时用Good.IsSpecialGoods == 1表示有货
                var goodCarQuery = db.ShoppingCarts.Where(s => s.Students_Id.HasValue && s.Students_Id == studentId && s.Good.IsSpecialGoods == 1)
                                            .Select(s => new MShoppingCart
                                            {
                                                Id = s.ID,
                                                StudentId = s.Students_Id.Value,
                                                GoodsName = s.Good.Goods_Name,
                                                GoodsId = s.Good.Goods_Id,
                                                GoodsAmount = s.Goods_Amount.HasValue ? s.Goods_Amount.Value : 0
                                            });

                shoppingCarts = goodCarQuery.ToList();

                // 通过每个shoppingCarts中的goodID获取所有的食物详情               
                foreach (MShoppingCart sCar in shoppingCarts)
                {
                    var g = db.Goods.Single(s => s.Goods_Id == sCar.GoodsId);
                    sCar.Good = new MGoods
                            {
                                Id = g.Goods_Id,
                                GoodsName = g.Goods_Name,
                                BrandName = g.Brand.Brand_Name,
                                GoodsInfo = g.Goods_info,
                                GoodsPictureUrl = g.Goods_Pic,
                                Price = !g.Goods_Price.HasValue ? 0 : (double)g.Goods_Price.Value,
                                GoodsSamllCategoryName = g.Goodscategory_.GoodsCategory_S_Name,
                                GoodsStandard = g.Goods_Standard,
                                IsSpecialGoods = g.IsSpecialGoods == 1
                            };
                }
                #endregion

                #region 添加食物

                // 根据学生Id查询所有的食物购物车
                var foodCarQuery = db.ShoppingCartFoods.Where(s => s.Students_Id.HasValue && s.Students_Id == studentId)
                                                .Select(s => new MShoppingCartFoods
                                                {
                                                    StudentId = s.Students_Id.Value,
                                                    Id = s.ID,
                                                    FoodsAmount = s.Foods_Amount.HasValue ? s.Foods_Amount.Value : 0,
                                                    FoodsId = s.Food.Foods_Id,
                                                    FoodssName = s.Food.Foods_Name,
                                                    RestaurantName = s.Food.Restaurant.Restaurant_Name,
                                                    IsBusy = s.Food.Restaurant.IsBusy.HasValue ? (bool)s.Food.Restaurant.IsBusy.Value : false,

                                                });
                foodsCarts = foodCarQuery.ToList();
                //根据每一个食物购物车获取其中的食物详情
                foreach (MShoppingCartFoods fCar in foodsCarts)
                {
                    var query = db.Foods.Where(s => s.Foods_Id == fCar.FoodsId)
                                    .Select(s => new MFoods
                                    {
                                        FoodId = s.Foods_Id,
                                        FoodName = s.Foods_Name,
                                        Price = s.Foods_Price.HasValue ? (double)s.Foods_Price.Value : 0,
                                        RestaruantAddress = s.Restaurant.Restaurant_Address ?? "",
                                        RestaurantName = s.Restaurant.Restaurant_Name,
                                        IsBusy = s.Restaurant.IsBusy.HasValue ? (bool)s.Restaurant.IsBusy.Value : false,
                                        FoodPicture = s.Foods_Pic ?? ""
                                    });
                    // fCar.Food = new MFoods();
                    fCar.Food = query.FirstOrDefault();
                }

                #endregion

                allThings.FoodsCarList = foodsCarts;
                allThings.GoodCarList = shoppingCarts;
            }

            return JsonConvert.SerializeObject(allThings);
            // return allThings;
        }
    }
}